const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    btns: [],
    section_id: 0,
    tabbar: {},
    cons: []
  },

  toggle: function(e) {
    var that = this;
    var _btns = that.data.btns;
    this.setData({
      //设置active的值为用户点击按钮的索引值
      section_id: e.currentTarget.dataset.index
    })
    that.getKmappingSubject(_btns[e.currentTarget.dataset.index].id);
  },
  clickHandle: function(e) {
    this.setData({
      //设置active的值为用户点击按钮的索引值
      // section_id: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.changeTabBar();
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getTagList&token=' + wx.getStorageSync('token'),
      dataType: 'json',
      method: 'GET',
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _btns = _res.data;
          that.setData({
            'btns': _btns
          })
          that.getKmappingSubject(_btns[0].id);
        }
      }
    })
  },
  /**
 * 扫描条形码或二维码
 */
  sweepCode: function () {
    var _this = this,
      user_id = wx.getStorageSync('userId');
    // wx.navigateTo({
    //             url: '/pages/testImg/testImg?app_id=' + 1101 + '&paper_download_task_id=' + 2249 + '&page=' + 1 + '&download_task_id=' + 332
    //           })
    wx.scanCode({
      onlyFromCamera: true,//仅仅相机
      success: (res) => {
        console.log(res);
        var _result = res.result;
        if (res.path) {
          var path = res.path.replace('code=', ''),
            pathIndex = path.indexOf('?'),
            parseUrl = path.substr(pathIndex + 1, path.length);
          var scene = decodeURIComponent(parseUrl).replace(/'/g, "");
          _result = scene.split("+");
          wx.request({
            url: app.globalData.URL + '/kmapping/WebAppAjax/checkPageStatus?' + app.globalData.URLBOOL,
            method: 'GET',
            data: {
              download_task_id: _result[3],
              paper_download_task_id: _result[1],
              user_id: user_id,
              wechat_flag: 1
            },
            success: function (_res) {
              var res = _res.data;
              if (res.error_code == 0) {
                wx.navigateTo({
                  url: '/' + path
                })
              } else {
                wx.showToast({
                  title: res.error_msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else {
          _result = _result.split("&");
          if (_result[0] != 1101 && _result[3] != 1101) {
            wx.showToast({
              title: '该套试题未启用!',
              icon: 'none',
              mask: true,
              duration: 2000
            })
            _this.sweepCode();
          } else {
            wx.request({
              url: app.globalData.URL + '/kmapping/WebAppAjax/checkPageStatus?' + app.globalData.URLBOOL,
              method: 'GET',
              data: {
                download_task_id: _result[3],
                paper_download_task_id: _result[1],
                user_id: user_id,
                wechat_flag: 1
              },
              success: function (_res) {
                var res = _res.data;
                if (res.error_code == 0) {
                  wx.navigateTo({
                    url: '/pages/testImg/testImg?online=0&app_id=' + _result[0] + '&paper_download_task_id=' + _result[1] + '&page=' + _result[2] + '&download_task_id=' + _result[3]
                  })
                } else {
                  wx.showToast({
                    title: res.error_msg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            })
          }
        }

      },
      //错误返回
      fail: (res) => {
      }
    })
  },
  getKmappingSubject: function(gradeId) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getKmappingSubject&token=' + wx.getStorageSync('token'),
      dataType: 'json',
      method: 'GET',
      data: {
        grade_id: gradeId
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _cons = _res.data;
          that.setData({
            'cons': _cons
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getKmappingSubject&token=' + wx.getStorageSync('token'),
      dataType: 'json',
      method: 'GET',
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _cons = _res.data;
          that.setData({
            'cons': _cons
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})